#include "graphic_atlas_renderer.h"
/*--------------------------------------------------------------------------------*/
#include "graphic_vertex_buffer.h"
/*--------------------------------------------------------------------------------*/
VOID GRAPHIC_ATLAS_RENDERER::Clear()
{
	VertexTable.Clear();
}
/*--------------------------------------------------------------------------------*/
VOID GRAPHIC_ATLAS_RENDERER::ReserveQuads( const COUNT count )
{
	VertexTable.ReserveItemCount( count * 6 );
}
/*--------------------------------------------------------------------------------*/
VOID GRAPHIC_ATLAS_RENDERER::AddQuad( const MATH_VECTOR_2 & position, const MATH_VECTOR_2 & extent, INT id, REAL /*angle*/ )
{
	COUNT
		current_count = VertexTable.GetItemCount();
	REAL
	    	half_size_x( extent.X * 0.5f ),
	    	half_size_y( extent.Y * 0.5f );

	VertexTable.SetItemCount( current_count + 6 );

	VERTEX
		*vertex_table = & VertexTable[ current_count ];


    vertex_table[0].Position = position + MATH_VECTOR_2( - half_size_x, + half_size_y );
    vertex_table[1].Position = position + MATH_VECTOR_2( + half_size_x, + half_size_y );
    vertex_table[2].Position = position + MATH_VECTOR_2( + half_size_x, - half_size_y );
    vertex_table[0].UV.Set( Atlas->GetUVCoord( id ).Start.X, Atlas->GetUVCoord( id ).End.Y );
    vertex_table[1].UV.Set( Atlas->GetUVCoord( id ).End.X, Atlas->GetUVCoord( id ).End.Y );
    vertex_table[2].UV.Set( Atlas->GetUVCoord( id ).End.X, Atlas->GetUVCoord( id ).Start.Y );

    // Triangle 2
    vertex_table[3].Position = vertex_table[2].Position;
    vertex_table[4].Position = position + MATH_VECTOR_2( - half_size_x, - half_size_y );
    vertex_table[5].Position = vertex_table[0].Position;
    vertex_table[3].UV = vertex_table[2].UV;
    vertex_table[4].UV.Set( Atlas->GetUVCoord( id ).Start.X, Atlas->GetUVCoord( id ).Start.Y );
    vertex_table[5].UV = vertex_table[0].UV;
}
/*--------------------------------------------------------------------------------*/
VOID GRAPHIC_ATLAS_RENDERER::Render()
{
	if( VertexTable.GetItemCount() )
	{
		Atlas->BindTexture();

		GRAPHIC_VERTEX_BUFFER::EnableVertexArray( ( REAL * ) VertexTable.GetArray(), 2, sizeof( VERTEX ) );
		GRAPHIC_VERTEX_BUFFER::EnableTexCoordArray( ( REAL * ) & VertexTable[0].UV , 2, sizeof( VERTEX ) );
		GRAPHIC_VERTEX_BUFFER::Draw( VertexTable.GetItemCount() );
	}
}
/*--------------------------------------------------------------------------------*/

